<?php
namespace app\flash\controller;
use think\Controller;
use think\Db;
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: *");
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http动作
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
    exit;
}

class Hangqing extends Controller
{
    public function index(){
        $input = input();
        //总条数
        $count = db('currency_hangqing')->count();
        if(empty($input['limit'])){
            $limit = 20;
        }elseif($input['limit']=='all'){
            //查询这个表一共有多少条数据
            $limit = $count;
        }else{
            $limit = $input['limit'];
        }
        $order = array();
        $where = array();
        if(!empty($input['order'])){
            if($input['order']=='zhang'){
                $where[]=['change_percent','>=',0];
                $order['change_percent'] = 'desc';
            }
            if($input['order']=='die'){
                $where[]=['change_percent','<=',0];
                $order['change_percent'] = 'asc';
            }
            if($input['order']=='chengjiaoe'){
                $order['vol'] = 'desc';
            }
            if($input['order']=='turnoverrate'){
                $order['turnoverrate'] = 'desc';
            }
        }else{
            $where[]=['change_percent','>=',0];
            $order['change_percent'] = 'desc';
        }
        $res = db('currency_hangqing')
            ->alias('ch')
            ->join('currency c','ch.code = c.code')
            ->where($where)
            ->order($order)
            ->paginate($limit,false,['query'=>request()->param() ]);
        return json(msg(1,$res,'数据返回成功!'));
    }

    //汇率接口
    public function coin_rate(){
        $data = db('currency_rate')->select();
        return json(msg(1,$data,'汇率返回成功!'));
    }

    //币库接口
    public function biku(){
        $input = input();
        $order = array();
        if(!empty($input['order'])&&!empty($input['by'])){
            $order[$input['by']] = $input['order'];
        }else{
            $order['market_value'] = 'desc';
        }
        $res = Db::table('currency_hangqing')
            ->alias('ch')
            ->join('currency c','ch.code = c.code')
            ->order($order)
            ->paginate(20,false,['query'=>request()->param() ]);
        return json(msg(1,$res,'数据返回成功!'));
    }

    public function jiaoyisuo(){
        $res = Db::table('bourse')
            ->order('bourse_business desc')
            ->field('id,bourse_name,bourse_logo,bourse_business,bourse_type')
            ->paginate(20,false,['query'=>request()->param() ]);
        return json(msg(1,$res,'交易所数据返回成功!'));
    }

    //货币详情
    public function huobi_detail(){
        $code =  input('code');
        $currency = Db::name('currency')->where('code',$code)->find();
        return json(msg(1,$currency,'货币详情返回成功'));
    }
}
